package com.example.studyPlatform.exception.handler;

import com.example.studyPlatform.config.ThemeConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
@Slf4j
@ControllerAdvice
@AllArgsConstructor
public class ControllerExceptionHandler {
    ThemeConfig themeConfig;
    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandler(HttpServletRequest req, Exception e) {
        log.error("Request : {},Exception : {}", req.getRequestURI(), e);
        ModelAndView mv = new ModelAndView();
        mv.addObject("url", req.getRequestURI());
        mv.addObject("error", e);
        mv.setViewName(themeConfig.render("error/error"));
        return mv;
    }
}
